//
//  CZRefreshControl.swift
//  微博
//
//  Created by 田凯 on 16/4/22.
//  Copyright © 2016年 田凯. All rights reserved.
//

import UIKit
enum CZRefreshControlType: Int{
    case Normal = 0
    case Pulling = 1
    case Refreshing = 2
}

class CZRefreshControl: UIControl {
    
    var czState : CZRefreshControlType = .Normal{
    
        didSet{
            switch czState{
            case .Normal:
//                print("正常")
                messageLabel.text = "正常"
                if oldValue == .Refreshing{
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    scrollView?.contentInset.top -= 50

                })
                 }
                
            case .Pulling:
//                print("松手刷新")
                messageLabel.text = "松手刷新"

            case .Refreshing:
//                print("刷新中")
                messageLabel.text = "刷新中"
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.scrollView?.contentInset.top += 50

                    }, completion: { (_) -> Void in
                        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)

                })
                
   
                
            }
        
        }
    
    }
    
    var scrollView : UIScrollView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 设置视图
    private func setupUI(){
    self.backgroundColor = RGB(red: 55, green: 185, blue: 65)
        //frame 
    addSubview(messageLabel)
    
    }
    func endRefreshing(){
    czState = .Normal
    
    }
    
    // 设置frame
    
    override func layoutSubviews() {
        self.frame = CGRect(x: 0, y: -50, width: ScreenW, height: 50)
        messageLabel.frame = CGRect(x: 0, y: 0, width: ScreenW, height: 50)
    }
    
    //监听
    override func willMoveToSuperview(newSuperview: UIView?) {
//        print(newSuperview)
        
        // 判断父控件 是否存在 切 是否 为 UIScrollView
        if let scrollView = newSuperview as? UIScrollView{
            
            self.scrollView = scrollView
//        scrollView.contentOffset
            //通过 kvo  监听 父控件的属性的改变
            scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
        
        }
        
    }
    // 监听 属性的改变
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        let contentOffsetY = scrollView?.contentOffset.y
//        print(scrollView?.dragging)
        if scrollView!.dragging{
            if contentOffsetY > -114 && czState == .Pulling {
//                print("正常")
                czState = .Normal
            }else if contentOffsetY <= -114 && czState == .Normal {
//                print("songshoushuaxin ")
                czState = .Pulling
            }
            

        
        }else{
            if czState == .Pulling{
            czState = .Refreshing
//                print("书信中")
            
            }
            
        
        
        }
        
        
        
        
    }
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.whiteColor()
        lab.font = UIFont.systemFontOfSize(14)
        lab.text = "正常"
        lab.textAlignment = .Center
        return lab
        
        
        }()
    
    
    deinit{
    self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    
    }
    

}
